package com.zhuoan.im.server.handler;

import com.zhuoan.im.server.command.ImStatus;
import com.zhuoan.im.webapp.biz.FriendsApplyBiz;
import com.zhuoan.im.webapp.dao.model.FriendsApplyInfo;
import com.zhuoan.im.webapp.dao.model.reqbody.FriendsApplyBody;
import com.zhuoan.im.webapp.dao.model.respbody.FriendsApplyRespBody;
import org.apache.commons.lang3.StringUtils;
import org.jim.common.ImPacket;
import org.jim.common.packets.Command;
import org.jim.common.packets.RespBody;
import org.jim.common.utils.JsonKit;
import org.jim.server.command.AbCmdHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.tio.core.ChannelContext;

import javax.annotation.Resource;
import java.util.List;

/**
 * FriendsListReqHandler
 *
 * @author weixiang.wu
 * @date 2018-10-17 19:51
 **/
@RestController
public class FriendsApplyOperateHandler extends AbCmdHandler {

    private final static Logger logger = LoggerFactory.getLogger(FriendsApplyOperateHandler.class);

    @Resource
    private FriendsApplyBiz friendsApplyBiz;


    @Override
    public Command command() {
        return Command.addAndGet(com.zhuoan.im.server.command.Command.FRIENDS_APPLY_OPERATE_REQ.getResMessage(), com.zhuoan.im.server.command.Command.FRIENDS_APPLY_OPERATE_REQ.getResCode());
    }

    @Override
    public ImPacket handler(ImPacket packet, ChannelContext channelContext) throws Exception {

        // 1.判断是否已经登录
        String appliedUserId = channelContext.getUserid();
        if (appliedUserId == null) {
            return new ImPacket(Command.addAndGet(com.zhuoan.im.server.command.Command.FRIENDS_APPLY_OPERATE_REQ.getResMessage(),
                com.zhuoan.im.server.command.Command.FRIENDS_APPLY_OPERATE_REQ.getResCode()), JsonKit.toJsonBytes("请您先登录"));
        }
        // 2.必要参数校验
        FriendsApplyBody friendsApplyBody = JsonKit.toBean(packet.getBody(), FriendsApplyBody.class);
        if (StringUtils.isBlank(String.valueOf(friendsApplyBody.getApplyUserId())) &&
            StringUtils.isBlank(String.valueOf(friendsApplyBody.getOperate()))) {
            return new ImPacket(org.jim.common.packets.Command.addAndGet(com.zhuoan.im.server.command.Command.FRIENDS_APPLY_OPERATE_REQ.getResMessage(),
                com.zhuoan.im.server.command.Command.FRIENDS_APPLY_OPERATE_REQ.getResCode()), JsonKit.toJsonBytes("参数缺失"));
        }
        friendsApplyBody.setAppliedUserId(Long.valueOf(appliedUserId));

        // 3.当前用户 处理好友申请请求
        friendsApplyBiz.operateSingleUserApply(friendsApplyBody);

//        // 4.返回最新好友申请列表
//        List<FriendsApplyInfo> userApplyList = friendsApplyBiz.queryUserApplyList(Long.valueOf(appliedUserId));

        // 5.好友信息申请成功返回
        RespBody friendsApplyRespBody = new FriendsApplyRespBody(
            Command.addAndGet(com.zhuoan.im.server.command.Command.FRIENDS_APPLY_OPERATE_RES.getResMessage(),
                com.zhuoan.im.server.command.Command.FRIENDS_APPLY_OPERATE_RES.getResCode())
            , ImStatus.C10024);

        return new ImPacket(friendsApplyRespBody.toByte());
    }
}
